﻿#ifndef QTVPLUGIN_GRID_H
#define QTVPLUGIN_GRID_H

#include <QWidget>
#include <QTranslator>
#include <QVector>
#include <QStandardItemModel>
#include <QSqlQueryModel>
#include <QSqlQuery>
#include <QStandardItemModel>
#include <QTimerEvent>
#include <QFuture>
#include <QFutureWatcher>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <atomic>
#include "../qtviewer_planetosm/osmtiles/layer_interface.h"
#include "../qtviewer_planetosm/osmtiles/viewer_interface.h"
#include "orbit_elems.h"
namespace Ui {
	class qtvplugin_orbit;
}
using namespace QTVOSM;

class qtvplugin_orbit : public QWidget,public layer_interface
{
	Q_OBJECT
	Q_PLUGIN_METADATA(IID OSMLayerInterface_iid )
	Q_INTERFACES(QTVOSM::layer_interface)
public:
	qtvplugin_orbit(QWidget *parent = 0);
	~qtvplugin_orbit();
	//Properties
	bool		is_active() override;
	void		set_active(bool ab) override;
	bool		is_visible() override;
	void		set_visible(bool vb) override;
	QString		get_name() override;
	void		set_name(QString vb) override;
	QWidget *	get_propWindow() override {return this;}
	QMap<QString, QVariant> call_func(const  QMap<QString, QVariant> & /*paras*/) override;
private:
	int m_nInstance;
	//International Translator
	QTranslator				pluginTranslator;
	Ui::qtvplugin_orbit *	ui;
	//the pointer to main MAP functions
	viewer_interface *		m_pVi;
	//Flags
	bool					m_bVisible;
	bool					m_bActive;
	//Lib Name
	QString					m_SLLibName;
	QString					m_SLLibPath;
	bool	loadTranslation();
protected:
	void timerEvent(QTimerEvent * evt) override;
	layer_interface *	load_initial_plugin(QString strSLibPath,viewer_interface  * ptrviewer) override;
	QWidget *			load_prop_window() override;
	void				load_retranslate_UI() override;
	void				load_ini();
	void				save_ini();
	QString				ini_file();
protected:
	//Events
	void cb_paintEvent( QPainter * pImage ) override;
	bool cb_mouseMoveEvent(QMouseEvent *) override;
	bool cb_mousePressEvent(QMouseEvent *) override;
	//Event filters
	bool cb_event(const QMap<QString, QVariant>) override;
protected:
	int m_nTraceTimer = -1;
	bool m_needUpdate = false;
	//!===========TLE MANAGER============
	QSqlQueryModel * m_pTleMod {nullptr};
	QSqlQueryModel * m_pTleModDetail{nullptr};
	QNetworkAccessManager * m_pDownloadManager{nullptr};
	//!===========TRACE==================
	QSqlQueryModel * m_pTraceMod {nullptr};
	QStandardItemModel * m_pSatPosSpeedMod  {nullptr};
	QVector<tag_trace_task> m_tasks_trace;
	QFuture<void> m_future_trace;
	std::atomic_bool m_bool_trace_caling {false};
	QMap<QString,bool> m_markTraceNames;
	std::vector<double> m_trace_beta;
#if QT_VERSION <= 0x060600
	QFutureWatcher<void> m_watcher;
protected slots:
	void slot_cal_finished();
#endif
protected:
	//!===========TLE MANAGER============
	void init_tle_mod();
	bool confirm_tle_conn(bool reset_gui);
	void loadFromTextStream(QByteArray text);
protected slots:
	void on_download_finished(QNetworkReply * reply);
protected:
	//!===========TRACE==================
	void init_trace_mod();
	bool confirm_trace_conn(bool reset_gui);
	void cal_sat_pv();
	void cal_trace(tag_trace_task & t);
	void disp_trace();
	void fini_trace_map();
	void update_trace_map(tag_trace_task & t);
	void report_trace_evt(tag_trace_task & t);

private slots:
	void on_pushButton_orbit_refresh_clicked();
	void on_toolButton_orbit_tle_db_clicked();
	void on_pushButton_orbit_tle_update_clicked();
	void on_pushButton_tle_orbit_del_clicked();
	void on_toolButton_orbit_tle_update_clicked();
	void on_tableView_orbit_tle_db_clicked(const QModelIndex &index);
	void on_pushButton_tle_orbit_del_sat_clicked();
	void on_pushButton_orbit_tle_find_clicked();
	void on_pushButton_tle_orbit_track_add_clicked();
	void on_checkBox_track_clicked();
	void on_dateTimeEdit_orbit_time_dateTimeChanged(const QDateTime &dateTime);
	void on_pushButton_orbit_track_remove_clicked();
	void on_pushButton_orbit_trace_find_clicked();
	void on_pushButton_orbit_ob_getpos_clicked();
	void on_tableView_orbit_trace_pos_clicked(const QModelIndex &index);
};

#endif // QTVPLUGIN_GRID_H


